﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    public class ProuductionController : Controller
    {
        private readonly IProductionServices productionServices;

        public ProuductionController(IProductionServices productionServices)
        {
            this.productionServices = productionServices;
        }

        public IActionResult ProuductionView()
        {
            return View();
        }

        public IActionResult AddProuductionView()
        {
            return View();
        }

        public IActionResult ProductionGanttView()
        {
            return View();
        }

        /// <summary>
        /// 添加生产工单
        /// </summary>
        /// <param name="prodction"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddProdction([FromBody] ProdctionDTO prodction)
        {
            return await productionServices.AddProdction(prodction);
        }

        /// <summary>
        /// 删除生产工单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteProdction(int id)
        {
            return await productionServices.DeleteProdction(id);
        }

        /// <summary>
        /// 获取生产工单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetProdctionById(int id)
        {
            var prodction = await productionServices.QueryProdction(id);
            return Json(new { data = prodction });
        }

        /// <summary>
        /// 生产工单列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowProdction(int pageIndex = 1, int pageSize = 10, string? orderName = "")
        {
            var list=await productionServices.ShowProdction(pageIndex, pageSize, orderName);
            var result = list.OrderByDescending(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code=0,
                msg="",
                count=list.Count,
                data=result
            });
        }

        /// <summary>
        /// 编辑生产工单
        /// </summary>
        /// <param name="prodction"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateProdction([FromBody]ProdctionDTO prodction) 
        {
        return await productionServices.UpdateProdction(prodction);
        }


    }
}
